package com.yinduo.yongyou.activity;


import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yinduo.yongyou.R;
import com.yinduo.yongyou.adpter.PhoneAdapter;
import com.yinduo.yongyou.callBack.CommonCallback;
import com.yinduo.yongyou.databinding.SettingAddPhoneBinding;
import com.yinduo.yongyou.entity.Common;
import com.yinduo.yongyou.entity.Phone;
import com.yinduo.yongyou.listener.PhoneClickedListener;
import com.yinduo.yongyou.utils.HTTPCons;
import com.yinduo.yongyou.utils.MyOkHttpUtils;
import com.yinduo.yongyou.utils.SharedPreferencesUtil;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by yanxu 2016/4/1.
 * 添加手机号
 */
public class AddPhoneActivity extends BaseActivity implements HTTPCons {
    private ArrayList<Phone> phones;
    Context mContext;
    PhoneAdapter adapter;
    SettingAddPhoneBinding mBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(
                this, R.layout.setting_add_phone);
        mContext = AddPhoneActivity.this;
        initData();
        //设置RecyclerView
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));

        mBinding.title.setText("手机号码");
        mBinding.btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddPhoneActivity.this.finish();
            }
        });

    }

    public void initData() {
        Map<String, String> map = new LinkedHashMap<String, String>();
        map.put("userId", SharedPreferencesUtil.getValueByKey(mContext, "userId", ""));
        MyOkHttpUtils.okhttpPost(mContext, mobileList_action, map, new CommonCallback() {
            @Override
            public void onResponse(Common response) {
                boolean isSucess = response.isSuccess();
                if (isSucess) {
                    phones = new Gson().fromJson(String.valueOf(response.getErrors().get(0)), new TypeToken<ArrayList<Phone>>() {
                    }.getType());
                    if (phones != null) {
                        adapter = new PhoneAdapter(mContext, phones, new PhoneClickedListener() {
                            @Override
                            public void onPhoneClick(String id) {
//                            chooseMobile(id);
                                Intent intent = new Intent();
                                Bundle bundle = new Bundle();
                                bundle.putString("phone", id);
                                intent.putExtras(bundle);
                                setResult(RESULT_PHONE, intent);
                                finish();//此处一定要调用finish()方法
                            }
                        });
                        mBinding.recyclerView.setAdapter(adapter);
                    }

                }
            }

        });
    }

    //    public void chooseMobile(String mobile){
//        Map<String, String> map = new LinkedHashMap<String, String>();
//        map.put("userId", SharedPreferencesUtil.getValueByKey(mContext, "userId", ""));
//        map.put("mobile", mobile);
//        MyOkHttpUtils.okhttpPost(mContext, chooseMobile_action, map, new CommonCallback() {
//            @Override
//            public void onResponse(Common response) {
//                boolean isSucess = response.isSuccess();
//                if (isSucess) {
//
//                }
//            }
//
//        });
//    }
    @Override
    public void onBackPressed() {
        AddPhoneActivity.this.finish();
    }
}